package niuke;

/**
 * description:
 * author:张腾
 * date:2021-07-03
 */

/**
 * 给定一个链表，请判断该链表是否为回文结构。
 */
public class NC96 {
    public boolean isPail (ListNode head) {
        // write code here
        ListNode fast=head, slow= head;
        while (fast.next!=null&&fast.next.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode nHead = slow.next;
        slow.next = null;
        ListNode newHead = reverse(nHead);
        while (head!=null&&newHead!=null){
            if (head.val!=newHead.val) return false;
            else {
                head=head.next;
                newHead=newHead.next;
            }
        }
        return true;
    }

    private ListNode reverse(ListNode head){
        ListNode pre = null;
        ListNode cur = head;
        while (cur!=null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}
